import axios from "axios";
import Vue from "vue";

console.log(process.env.NODE_ENV, 'process.env.NODE_ENV');

const instance = axios.create({
  // 基础请求URL - 开发环境使用代理，生产环境使用完整URL
  baseURL: window.location.origin.includes('localhost') ? window.location.origin : 'https://api.youdianstar.vn',
  // 响应超时的时间
  timeout: 30000,
  // 跨域
  withCredentials: true
});

// 免登录校验uri
// const ignoreURI = [
//   '/api/login',
//   '/api/common/configs',
//   '/api/login/country',
//   '/api/login/verification-code',
// ]

// 是否存在忽略URI
// function isIgnoreURI (uri) {
//   for (let i = 0; i < ignoreURI.length; i++) {
//     if (uri.startsWith(ignoreURI[i])) {
//       return true
//     }
//   }
//   return false
// }

// 请求拦截
instance.interceptors.request.use(
  config => {
    // if (!isIgnoreURI(config.url)) {
    // }
    // 添加请求头
    config.headers.token = localStorage.getItem("token") || "";
    config.headers["brezze-language"] = localStorage.getItem("lang") || "VI";
    config.headers["platform-id"] = localStorage.getItem("platformId") || "1";
    config.headers["os"] = localStorage.getItem("phoneOS") || "";
    if (!config.headers["isDisLoading"]) $nuxt.$store.commit("startLoading");
    return config;
  },
  error => {
    // do something with request error

    $nuxt.$store.commit("endLoading");
    return Promise.reject(error);
  }
);

// 响应拦截
instance.interceptors.response.use(
  res => {
    // console.log(res)
    $nuxt.$store.commit("endLoading");
    if (res.data.code === "10401") {
      $nuxt.$store.commit("login_out");
      Vue.prototype.global.toast(window.vm.t("Login_expire"));
      return res.data;
    } else {
      return res.data;
    }
  },
  error => {
    // console.log('响应拦截错误', error)
    $nuxt.$store.commit("endLoading");
    const { response } = error;
    // console.log(response)
    if (response) {
      // 请求已发出，但是不在2xx的范围
      errorHandle(response.status, response.statusText);
      return Promise.reject(response);
    } else {
      Vue.prototype.global.toast(error.message);
      return Promise.reject(error);
      // 处理断网的情况
      // eg:请求超时或断网时，更新state的network状态
      // network状态在app.vue中控制着一个全局的断网提示组件的显示隐藏
      // 关于断网组件中的刷新重新获取数据，会在断网组件中说明
      // Vue.prototype.$store.commit('changeNetwork', false)
    }
  }
);

/**
 * get请求
 * @param uri
 * @param params
 * @param headers 请求头参数
 * @returns {Promise<unknown>}
 */
const get = (uri, params, headers) => {
  let paramsData;
  if (params) {
    // 拷贝对象
    paramsData = JSON.parse(JSON.stringify(params));
    // console.log(paramsData)
    if (paramsData) {
      // 处理时间范围参数
      if (paramsData.rangeTime && paramsData.rangeTime.length === 2) {
        paramsData.rangeTime = paramsData.rangeTime.join(",");
      }
    }
  }
  return new Promise((resolve, reject) => {
    instance
      .get(uri, {
        headers: headers,
        params: paramsData
      })
      .then(res => {
        resolve(res);
      })
      .catch(res => {
        reject(res);
      });
  });
};

/**
 * post请求
 * @param uri     请求路径
 * @param params  请求参数
 * @param headers 请求头参数
 * @returns {Promise<unknown>}
 */
const post = (uri, params, headers) => {
  if (isObject(headers)) {
    headers["Content-Type"] = "application/json; charset=UTF-8";
  } else {
    headers = {
      "Content-Type": "application/json; charset=UTF-8"
    };
  }
  return new Promise((resolve, reject) => {
    instance
      .post(uri, params, {
        headers: headers
      })
      .then(res => {
        resolve(res);
      })
      .catch(res => {
        reject(res);
      });
  });
};

/**
 * patch请求
 * @param uri     请求路径
 * @param params  请求参数
 * @param headers 请求头参数
 * @returns {Promise<unknown>}
 */
const patch = (uri, params, headers) => {
  if (isObject(headers)) {
    headers["Content-Type"] = "application/json; charset=UTF-8";
  } else {
    headers = {
      "Content-Type": "application/json; charset=UTF-8"
    };
  }
  return new Promise((resolve, reject) => {
    instance
      .patch(uri, params, {
        headers: headers
      })
      .then(res => {
        resolve(res);
      })
      .catch(res => {
        reject(res);
      });
  });
};

/**
 * put请求
 * @param uri     请求路径
 * @param params  请求参数
 * @param headers 请求头参数
 * @returns {Promise<unknown>}
 */
const put = (uri, params, headers) => {
  if (isObject(headers)) {
    headers["Content-Type"] = "application/json; charset=UTF-8";
  } else {
    headers = {
      "Content-Type": "application/json; charset=UTF-8"
    };
  }
  headers["Content-Type"] = "application/json; charset=UTF-8";
  return new Promise((resolve, reject) => {
    instance
      .put(uri, params, {
        headers: headers
      })
      .then(res => {
        resolve(res);
      })
      .catch(res => {
        reject(res);
      });
  });
};

/**
 * delete请求
 * @param uri     请求路径
 * @param params  请求参数
 * @param headers 请求头参数
 * @returns {Promise<unknown>}
 */
const del = (uri, params, headers) => {
  return new Promise((resolve, reject) => {
    instance
      .delete(uri, {
        headers: headers,
        params: params
      })
      .then(res => {
        resolve(res);
      })
      .catch(res => {
        reject(res);
      });
  });
};

/**
 * 上传文件
 * @param params  请求参数
 * @param headers 请求头参数
 * @returns {Promise<unknown>}
 */
const uploadFile = (params, headers) => {
  // console.log('上传文件====>', params, headers)
  if (isObject(headers)) {
    headers["Content-Type"] = "multipart/form-data;";
  } else {
    headers = {
      "Content-Type": "multipart/form-data;"
    };
  }
  let formData;
  if (params instanceof FormData) {
    formData = params;
  } else {
    formData = new FormData();
    formData.append("files", params.file);
  }
  return new Promise((resolve, reject) => {
    instance
      .request({
        url: "/manage/common/files/upload",
        method: "post",
        headers: headers,
        data: formData
      })
      .then(res => {
        resolve(res);
      })
      .catch(res => {
        reject(res);
      });
  });
};

/**
 * 请求失败后的错误统一处理
 * @param {Number} status 请求失败的状态码
 */
const errorHandle = (status, message) => {
  // 状态码判断
  switch (status) {
    // 401: 未登录状态，跳转登录页
    case 401:
      Vue.prototype.global.toast(window.vm.t("Login_expire"));
      $nuxt.$store.commit("login_out");
      break;
    // 403 token过期, 清除用户信息,并跳转登录页
    case 403:
      Vue.prototype.global.toast(window.vm.t("Login_expire"));
      $nuxt.$store.commit("login_out");
      break;
    // 404请求不存在
    case 404:
      Vue.prototype.global.toast("404 request does not exist");
      $nuxt.$store.commit("login_out");
      break;
    default:
      Vue.prototype.global.toast(message);
    // console.log(message)
  }
};

/**
 * 是否是object对象
 * @param val
 * @returns {boolean}
 */
function isObject(val) {
  return val && Object.prototype.toString.call(val) === "[object Object]";
}

export { get, post, put, del, uploadFile };
